package cn.yhjz.platform.system.controller;

import cn.yhjz.common.util.IdUtil;
import cn.yhjz.platform.system.service.ISysMenuService;
import cn.yhjz.platform.system.entity.SysMenu;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.yhjz.platform.system.vo.SysMenuVo;
import cn.yhjz.common.AjaxResponse;
import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/sysmenu")
@PreAuthorize("hasAuthority('system:menu')")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @PostMapping("/list")
    public List<SysMenuVo> list(@RequestBody SysMenu sysMenu) {
        return sysMenuService.select(sysMenu);
    }

    @PostMapping("/page")
    public AjaxResponse page(@RequestBody SysMenuVo vo) {
        SysMenu entity = new SysMenu();
        BeanUtils.copyProperties(vo, entity);
        return AjaxResponse.success(sysMenuService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity));
    }

    @PostMapping("/addOrUpdate")
    public AjaxResponse add(@RequestBody SysMenu entity) {
        if (entity.getId() != null) {
            entity.setUpdatedBy(LoginUtil.getCurrentUser().getUserCode());
            entity.setUpdatedTime(new Date());
            return AjaxResponse.success(sysMenuService.updateById(entity));
        } else {
            entity.setMenuCode(IdUtil.generate());
            entity.setCreatedBy(LoginUtil.getCurrentUser().getUserCode());
            entity.setCreatedTime(new Date());
            return AjaxResponse.success(sysMenuService.insert(entity));
        }
    }

    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(sysMenuService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(sysMenuService.deleteById(id));
    }

    /**
     * 获得树形的菜单结构
     *
     * @return
     */
    @RequestMapping("/loadMenuTree")
    public AjaxResponse loadMenuTree() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.selectMenu(null);
        List<SysMenuVo> baseList = buildTree(sysMenuVoList);
        return AjaxResponse.success(baseList);
    }

    /**
     * 获得所有权限的树形结构
     *
     * @return
     */
    @RequestMapping("/loadPermissionTree")
    public AjaxResponse loadPermissionTree() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.select(null);
        List<SysMenuVo> baseList = buildTree(sysMenuVoList);
        return AjaxResponse.success(baseList);
    }

    /**
     * 加载当前用户的权限菜单
     * @return
     */
    @RequestMapping("/loadUserMenuTree")
    public AjaxResponse loadUserMenuTree(HttpSession session){
        Object sysUser = session.getAttribute("currentUser");
        return null;
    }
    /**
     * 把列表整理成树形结构
     * @param sysMenuVoList
     * @return
     */
    public static List<SysMenuVo> buildTree(List<SysMenuVo> sysMenuVoList){
        for (SysMenuVo sysMenuVo : sysMenuVoList) {
            for (SysMenuVo sysMenuVoInner : sysMenuVoList) {
                if (StrUtil.equals(sysMenuVoInner.getParentCode(), sysMenuVo.getMenuCode())) {
                    if(CollUtil.isEmpty(sysMenuVo.getChildren())){
                        sysMenuVo.setChildren(new ArrayList<>());
                    }
                    sysMenuVo.getChildren().add(sysMenuVoInner);
                }
            }
        }
        List<SysMenuVo> baseList = new ArrayList<>();
        for (SysMenuVo sysMenuVo : sysMenuVoList) {
            if("root".equals(sysMenuVo.getParentCode()) || StrUtil.isBlank(sysMenuVo.getParentCode())){
                baseList.add(sysMenuVo);
            }
        }
        return baseList;
    }
}
